let {addCarts,find,deleteCarts}=require('../server/cart.js')

async function add(ctx){
     try{
       let user_id=ctx.state.user.id
    //    let {goods_id,num,isSelect}=ctx.request.body
       let res=await addCarts(user_id,ctx.request.body)
       if(res){
           ctx.body={
               code:200,
               message:"购物车添加成功",
               result:{
                   res
               }
           }
       }else{
        ctx.app.emit('error',{
            code:40000,
            message:"添加购物车失败",
            result:''
        },ctx)
       }
     }catch(error){
         console.log(error)
         ctx.app.emit('error',{
             code:40000,
             message:"添加购物车失败"+error,
             result:''
         },ctx)
     }
}

async function findAll(ctx){
    try{
        let pageSize=ctx.request.body.pageSize||10
        let pageNum=ctx.request.body.pageNum||1
      let res=await find(pageSize,pageNum)
      if(res){
          ctx.body={
              code:200,
              message:'购物车获取成功',
              result:{
                  res
              }
          }
      }
    }catch(error){
        console.log(error)
        ctx.app.emit('error',{
            code:50000,
            message:"购物车获取失败",
            result:""
        },ctx)
    }
}

async function deleteCart(ctx){
    try{
       let {id}=ctx.request.body
       let res=await deleteCarts(id)
       ctx.body={
           code:200,
           message:"删除成功",
           reslut:{
               res
           }
       }
    }catch(error){
        console.log(error)
        ctx.app.emit('error',{
            code:50000,
            message:"删除购物车失败"+error,
            result:""
        },ctx)
    }
}
module.exports={
    add,
    findAll,
    deleteCart
}